* WINDOWS PREAMBLE. 
#delimit ;
global rawDir "LOCATION OF RAW CPS DATA";
global dataDir "WORKING DIRECTORY";
global currDir "PROGRAM DIRECTORY";
cd "$currDir" ;
capture log close ;
clear all ;
log using 6_SingleRestrictions_regPrep.log, replace text;


***************************************************************************;
* REGRESSION PREP ;
***************************************************************************;

local beginTime "DateTime: $S_DATE $S_TIME";
set more off ;
set linesize 200 ;
set mem 5g ;

cd "$dataDir" ;




***************************************************************************;
* MERGE POLICY MEASURES, AND OTHER CONTROLS;
***************************************************************************;

use cpsAfterTaxsim_singleRestrictions ;

* CHILDREN ;
gen children = min(qualChild,3) ;
tab children ;

* TAX - THESE USE CPS DATA ;
sort year state children ;
merge m:1 year state children using simulatedmeasures ;
drop if _merge == 2;
drop _merge children ;
summ *SIV* ;

* MERGE THE MANY WAIVER VARIABLES ;
sort state ;
merge m:1 state using afdcwaivers ;
drop if _merge==2 ;
drop _merge ;
summ afdcwaiver* ;

* TRANSFER PROGRAM PARAMETERS FROM UKPRC - NOT SIMULATED;
sort year state ;
merge m:1 year state using state_level_variables_from_UKPRC ;
drop if _merge == 2 ;
drop _merge ;
summ state year pop emp unemp unempr gsp afdcmb2 afdcmb3 afdcmb4 
	fsmb1 fsmb2 fsmb3 fsmb4 ssimb eitcmax0 eitcmax1 eitcmax2 
	steitcr steitcref fedminwge stminwge ;

* MERGE ON FEDERAL POVERTY LINE ;
*gen othersandchildren = min(famsize , 9);
gen qualchild = min(qualChild , 8) ;
gen othersandchildren = 1+qualchild;
sort year othersandchildren qualchild ;
merge m:1 year othersandchildren qualchild using FPL ;
drop if _merge ==2 ;
drop _merge ;
summ fpl if year == 1993 & othersandchildren==2 & qualchild == 1 ;
replace fpl = 9960 if year == 1993 & othersandchildren==2 & qualchild == 1 ;
summ fpl if year == 1993 & othersandchildren==5 & qualchild == 3 ;
replace fpl = 17245 if year == 1993 & othersandchildren==5 & qualchild == 3 ;
summ fpl ;
drop qualchild ; 

* MERGE IN HOUSING SUBSIDY - 1984 onward;
replace year = year +1 ;
sort year serial pernum ;
merge 1:1 year serial pernum using housing_sub_cps ;
drop if _merge == 2;
drop _merge ;
replace year = year - 1 ;
gen houssub = f_houssub*12 ;
gen schlunch = f_mv_sl ;
summ f_houssub houssub;

* ADJUST VARIABLES THAT NEED IT FOR INFLATION. SOME ARE ALAREDAY DONE ;
foreach dollar of varlist gsp afdcmb2 afdcmb3 afdcmb4 
		fsmb1 fsmb2 fsmb3 fsmb4 ssimb eitcmax0 eitcmax1 eitcmax2
		fedminwge stminwge fpl houssub schlunch {;
	qui replace `dollar' = `dollar' * (232.957/cpi); 
	};



***************************************************************************;
* MAKING VARIABLES ;
***************************************************************************;

gen afdcmb = 0 ;
replace afdcmb = afdcmb2 if qualChild == 1 ;
replace afdcmb = afdcmb3 if qualChild == 2 ;
replace afdcmb = afdcmb4 if qualChild >= 3 ;
gen fsmb = 0 ;
replace fsmb = fsmb1 if qualChild == 0 ;
replace fsmb = fsmb2 if qualChild == 1 ;
replace fsmb = fsmb3 if qualChild == 2 ;
replace fsmb = fsmb4 if qualChild >= 3 ;

gen minwage = fedminwge+stminwge ;

*replace famsize = min(famsize,5) ;
replace hhsize = min(hhsize-1-qualChild, 5)  ;


sort state year;
by state year: gen oneobs = _n ;
summ afdcmb3 if oneobs == 1 & year==1984, d ;
gen temp = afdcmb3>r(mean) if year==1984;
sort state;
by state : egen afdcmb3GTmean = max(temp);
tab afdcmb3GTmean ;
drop oneobs temp;

foreach var in jobs limit asset medi prov child {;
	gen byte wave_`var' = (year>=afdcwaiver`var');
	replace wave_`var' = 0 if wave_`var'==. ;
	gen byte waveXc_`var' = (wave_`var')*(qualChild>0) ;
	};
gen byte anywave = (wave_jobs+wave_limit+wave_asset+wave_medi+wave_prov+wave_child)>0 ;
gen byte anywaveXc = anywave*(qualChild>0) ;
summ afdcwaiver* wave_* waveXc_* anywave anywaveXc;

egen minyear = rowmin(afdcwaiver*) ;
gen waiver1995 = 0 ;
replace waiver1995 = 1 if minyear < 1995 & minyear!=. & minyear!=0;
tab waiver1995, missing;
drop minyear ;

foreach a of numlist 24(4)52 {;
	gen byte age_`a' = (age >= `a' & age <= `a'+3); 
	};
summ age age_* ;
*table year children [aw=wtsupp], c(mean age) ;

gen byte hgc_11 = (hgc < 12) ;
foreach e in 12 13 16 17 {;
	gen byte hgc_`e' = (hgc == `e') ;
	};
summ hgc hgc_* ;
*table year children [aw=wtsupp], c(mean hgc) ;
*table year children [aw=wtsupp], c(mean hgc_11) ;

gen byte white = race == 100 ;
gen byte hispanic = hispan!=0 ;
*tabstat white hispanic [aw=wtsupp], by(year) s(mean) c(v) ;

gen byte divorced = marst == 3 | marst == 4 ;

foreach c of numlist 0(1)3 {;   
	gen cI`c' = qualChild == `c' ;  
	};
replace cI3 = 1 if qualChild > 3 ;
summ cI* ;

*summ ;

save singleWomen_singleRestrictions, replace ;


*/
*;
disp "`beginTime'" ;
disp "DateTime: $S_DATE $S_TIME";
log close ;























